package com.egao.common.module.outer.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.utils.CompanyUtil;
import com.egao.common.module.outer.entity.OuterMutual;
import com.egao.common.module.outer.mapper.OuterMutualMapper;
import com.egao.common.module.outer.service.OuterMutualService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 校外双选会接口实现类
 * @author pyx
 */
@Service
public class OuterMutualServiceImpl extends ServiceImpl<OuterMutualMapper, OuterMutual> implements OuterMutualService {
    @Override
    public List<OuterMutual> listPage(PageParam<OuterMutual> pageParam) {
        //处理举办时间段数据
        pageParam = (PageParam<OuterMutual>) CompanyUtil.putApplyTime(pageParam);

        return baseMapper.listPage(pageParam);
    }

    @Override
    public JsonResult insertOuterMutual(OuterMutual outerMutual) {
        if (outerMutual == null) {
            throw new ParameterException("参数对象为空");
        }

        if (StrUtil.isBlank(outerMutual.getTitle())) {
            throw new ParameterException("标题不能为空");
        }
        if (StrUtil.isBlank(outerMutual.getOrganizer())) {
            throw new ParameterException("主办方不能为空");
        }
        if (StrUtil.isBlank(outerMutual.getHoldAddress())) {
            throw new ParameterException("举办地点不能为空");
        }
        if (outerMutual.getHoldTime() == null) {
            throw new ParameterException("举办时间不能为空");
        }

        baseMapper.insert(outerMutual);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult updateOuterMutual(OuterMutual outerMutual) {
        if (outerMutual == null) {
            throw new ParameterException("参数对象为空");
        }

        if (StrUtil.isBlank(outerMutual.getTitle())) {
            throw new ParameterException("标题不能为空");
        }
        if (StrUtil.isBlank(outerMutual.getOrganizer())) {
            throw new ParameterException("主办方不能为空");
        }
        if (StrUtil.isBlank(outerMutual.getHoldAddress())) {
            throw new ParameterException("举办地点不能为空");
        }
        if (outerMutual.getHoldTime() == null) {
            throw new ParameterException("举办时间不能为空");
        }

        baseMapper.updateById(outerMutual);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult deleteOuterMutual(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        if (removeById(id)) {
            return JsonResult.ok("删除成功");
        }

        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult getId(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        QueryWrapper<OuterMutual> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Integer count = baseMapper.selectCount(wrapper);

        if (count != 1) {
            throw new ParameterException("信息不存在");
        }

        OuterMutual outerMutual = baseMapper.selectById(id);
        return JsonResult.ok().setData(outerMutual);
    }
}
